//
//  MonthRecorderViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/8.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class MonthRecorderViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var noData : NoDataView?
    var table : UITableView?
    var source : Array<TurnOverModel> = [] {
        didSet{
            table!.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "月记录"
        self.naviHide = false
        self.defaultBackButton()
        self.view.backgroundColor = UIColor.white
        configTableView()
        loadData(count: 0)
        // Do any additional setup after loading the view.
    }
    
    private func configTableView()->Void{
        table = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: YXDeviceHeight - 64), style: .plain)
        table!.delegate = self
        table!.dataSource = self
        table!.register(MonthRecordCell.classForCoder(), forCellReuseIdentifier: "monthData")
        table!.allowsSelection = false
        table!.separatorStyle = .none
        self.view.addSubview(table!)
        noData = NoDataView.init(frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: YXDeviceHeight - 64))
        noData?.refreshBlock = {
            self.loadData(count: 1)
        }
        self.view.addSubview(noData!)
        table?.isHidden = true
        noData?.isHidden = true
    }
    
    func loadData(count : Int) -> Void {
        //得到数据(目前只加载3个月的数据)
        //渲染UI
        self.beginAnim()
        Manager.shared().getMonthsData(months: "3", success: {(model) in
            self.endAnim()
            if let mod = model as? MonthRecordModel {
                if mod.dataArr.count > 0{
                    self.noData?.isHidden = true
                    self.table?.isHidden = false
                    self.source = mod.dataArr
                } else {
                    self.noData?.isHidden = false
                    self.table?.isHidden = true
                    self.noData?.endRefresh()
                }
            }
        }, fail: {(code) in
            self.endAnim()
            self.commonAlertMsg(content: YXNetWorkFail)
            self.noData?.endRefresh()
        })
    }
    //TableView Delegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return source.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let count = source[indexPath.row].dataSrc.count
        return  CGFloat(count + 3) * 88 * YXHeightScale
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "monthData")!
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cel = cell as! MonthRecordCell
        cel.model = source[indexPath.row]
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
